package com.atguigu.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;

public class ParameterServlet extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        req.setCharacterEncoding("UTF-8");
        System.out.println("--------------doGet-----------------");
        //获取请求的参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String []hobby = req.getParameterValues("hobby");
//        String hobby = req.getParameter("hobby");

        System.out.println("username="+username);
        System.out.println("password="+password);
//        System.out.println("hobby="+hobby);
        System.out.println("hobby="+Arrays.asList(hobby));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置请求体的字符集为UTF-8,从而解决post请求的中文乱码问题
        //也要在获取请求参数之前调用才有效
        req.setCharacterEncoding("UTF-8");
        System.out.println("--------------doPost-----------------");
        //获取请求的参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String []hobby = req.getParameterValues("hobby");
//        String hobby = req.getParameter("hobby");

        System.out.println("username="+username);
        System.out.println("password="+password);
//        System.out.println("hobby="+hobby);
        System.out.println("hobby="+Arrays.asList(hobby));
    }
}
